package cn.com.nes.common.util;

import com.alibaba.fastjson2.JSONObject;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.request.GetRequest;

import java.net.URLEncoder;
import java.sql.SQLException;

public class TianyanchaNew {

    private static final String token = "09b0b53a-893a-4967-85a0-9011794433fb";

    public static JSONObject searchCompany(String word, int pageNum, int pageSize) {
        JSONObject companyObj = new JSONObject();
        String companyStr = "";
        try {
            String wordParsed = URLEncoder.encode(word, "UTF-8");
            StringBuilder url = new StringBuilder("http://open.api.tianyancha.com/services/open/search/2.0?word=").append(wordParsed)
                    .append("&pageSize=")
                    .append(pageSize)
                    .append("&pageNum=")
                    .append(pageNum)
                    .append(" ");
            GetRequest request = Unirest.get(url.toString());
            request = request.header("Authorization", token);
            HttpResponse<String> response = request.asString();
            companyStr = response.getBody();
            companyObj = ((JSONObject) JSONObject.parse(companyStr)).getJSONObject("result");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return companyObj;
    }

    public static int getPageount(int recordcount, int pagesize) throws SQLException {
        int pagecount = recordcount % pagesize == 0 ? recordcount / pagesize : recordcount / pagesize + 1;
        return pagecount;
    }

    public static JSONObject baseinfoCompany(String keyword) {
        JSONObject companyObj = new JSONObject();
        String companyStr = "";
        try {
            String wordParsed = URLEncoder.encode(keyword, "UTF-8");
            StringBuilder url = new StringBuilder("http://open.api.tianyancha.com/services/open/ic/baseinfoV3/2.0?keyword=").append(wordParsed);
            GetRequest request = Unirest.get(url.toString());
            request = request.header("Authorization", token);
            HttpResponse<String> response = request.asString();
            companyStr = response.getBody();
            companyObj = ((JSONObject) JSONObject.parse(companyStr)).getJSONObject("result");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return companyObj;
    }

    public static JSONObject getCompanyChange(String keyword) {
        JSONObject companyObj = new JSONObject();
        String companyStr = "";
        try {
            String wordParsed = URLEncoder.encode(keyword, "UTF-8");
            StringBuilder url = new StringBuilder("http://open.api.tianyancha.com/services/open/hi/ic/2.0?keyword=").append(wordParsed);
            GetRequest request = Unirest.get(url.toString());
            request = request.header("Authorization", token);
            HttpResponse<String> response = request.asString();
            companyStr = response.getBody();
            companyObj = ((JSONObject) JSONObject.parse(companyStr)).getJSONObject("result");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return companyObj;
    }

    public static JSONObject getCompanyUser(String keyword) {
        JSONObject companyObj = new JSONObject();
        String companyStr = "";
        try {
            String wordParsed = URLEncoder.encode(keyword, "UTF-8");
            StringBuilder url = new StringBuilder("http://open.api.tianyancha.com/services/open/ic/contact?keyword=").append(wordParsed);
            GetRequest request = Unirest.get(url.toString());
            request = request.header("Authorization", token);
            HttpResponse<String> response = request.asString();
            companyStr = response.getBody();
            companyObj = ((JSONObject) JSONObject.parse(companyStr)).getJSONObject("result");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return companyObj;
    }

    public static void main(String[] args) {

        String keyword = "北京永宝昌地源热泵中央空调技术有限责任公司";
        int pageNum = 1;
        int pageSize = 20;
        System.out.println(TianyanchaNew.searchCompany(keyword, pageNum, pageSize));
    }

}
